package com.eco.controller;


import com.eco.service.ChartsService;
import com.eco.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("charts")
@CrossOrigin
public class ChartsController {
    @Autowired
    ChartsService chartsService;
    @RequestMapping("chartsA")
    public Result chartA() {
        List<HashMap> list = chartsService.chartA();
        System.out.println(list);
        List nameList = new ArrayList<>();
        List countList = new ArrayList<>();
        for(HashMap item : list){
            nameList.add(item.get("project_name"));
            countList.add(item.get("total_liability"));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("project_name", nameList);
        result.put("total_liability", countList);

        return Result.success(result);
    }

    @RequestMapping("chartsB")
    public Result chartB() {
        List<HashMap> list = chartsService.chartB();
        List<String> nameList = new ArrayList<>();
        List<Double> incomeList = new ArrayList<>();
        List<Double> expenseList = new ArrayList<>();

        for (HashMap item : list) {
            nameList.add((String) item.get("project_name"));
            incomeList.add(item.get("total_income") == null ? 0.0 : ((Number) item.get("total_income")).doubleValue());
            expenseList.add(item.get("total_expense") == null ? 0.0 : ((Number) item.get("total_expense")).doubleValue());
        }

        Map<String, Object> result = new HashMap<>();
        result.put("project_name", nameList);
        result.put("total_income", incomeList);
        result.put("total_expense", expenseList);

        return Result.success(result);
    }

}


















